package com.cjjd.c.feature.mine.register;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cjjd.c.R;
import com.cjjd.c.base.ActionBarFragment;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.feature.mine.login.LoginActivity;
import com.cjjd.c.http.CallbackImpl;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.utils.ToastUtil;
import com.cjjd.c.view.MyEditText;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * ----------------------------------
 * Created by xyd on 2017/8/10.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class SetPsdFragment extends ActionBarFragment {
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_hint)
    TextView tvHint;
    @Bind(R.id.et_psd)
    MyEditText etPsd;
    @Bind(R.id.et_psd_again)
    MyEditText etPsdAgain;
    @Bind(R.id.checkbox)
    CheckBox checkbox;
    @Bind(R.id.tv_next)
    TextView tvNext;
    @Bind(R.id.checkboxwrapper)
    RelativeLayout checkboxwrapper;




    // 页面来自找回密码
    public static final int Page_from_findpsd = 0x00000001;
    private int page_from = 0;

    private String mobile;
    private String captcha;
    private String sessionId;
    private String psd;
    private String psd_again;


    @Override
    public Object getTitleString() {
        return null;
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_set_psd;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }

    @Override
    protected void initView() {
        super.initView();
        if (page_from == Page_from_findpsd) {
            tvTitle.setText("找回密码");
            tvHint.setText("请设置一个安全的新密码，过于简单的密码容易被人盗用");
            etPsd.setHint("请设置一个新的密码");
            etPsdAgain.setHint("请再输入一次您设置的密码");
            tvNext.setText("完成");
            checkboxwrapper.setVisibility(View.GONE);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

//    @OnClick(R.id.tv_next)
//    public void onViewClicked() {
//
//    }

    private void resetPsd() {

        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("mobile", mobile);
        h1.put("captcha", captcha);
        h1.put("sessionId", sessionId);
        h1.put("password", (psd));

        Call<HttpResult<String>> resetCustomerPwd = service.resetCustomerPwd(JsonUtils.convertParam("resetCustomerPwd", h1));
        resetCustomerPwd.enqueue(new CallbackImpl<String>() {
            @Override
            public void responseSuccess(Call<HttpResult<String>> call, Response<HttpResult<String>> response) {
                ToastUtil.showToast("密码重置成功，请登录");

                mContext.onBackPressed();
                mContext.onBackPressed();
            }
        });
    }

    private void setPsd() {
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("mobile", mobile);
        h1.put("captcha", captcha);
        h1.put("sessionId", sessionId);
        h1.put("password", (psd));

        Call<HttpResult<String>> customerRegister = service.customerRegister(JsonUtils.convertParam("customerRegister", h1));
        customerRegister.enqueue(new CallbackImpl<String>() {
            @Override
            public void responseSuccess(Call<HttpResult<String>> call, Response<HttpResult<String>> response) {
                Intent intent = new Intent(mContext, LoginActivity.class);
                startActivity(intent);
            }
        });
    }


    private boolean checkPsd() {

        psd = etPsd.getText().toString().trim();
        psd_again = etPsdAgain.getText().toString().trim();

        if (TextUtils.isEmpty(psd)) {
            ToastUtil.showLongToast(mContext, "请输入设置密码！");
            return false;
        }

        if (TextUtils.isEmpty(psd_again)) {
            ToastUtil.showLongToast(mContext, "请输入确认密码！");
            return false;
        }

        if (!psd.equals(psd_again)) {
            ToastUtil.showLongToast(mContext, "两次密码输入不一致！");
            return false;
        }

        return true;
    }


    public void setData(String mobile, String captcha, String sessionId) {
        this.mobile = mobile;
        this.captcha = captcha;
        this.sessionId = sessionId;
    }

    public void setStartFrom(int page_from_findpsd) {
        page_from = page_from_findpsd;
    }


    @OnClick({R.id.back, R.id.tv_next,R.id.tv_read_and_agree2,R.id.tv_read_and_agree})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                mContext.finish();
                break;
            case R.id.tv_next:
                if (checkPsd()) {

                    if (page_from == Page_from_findpsd) {
                        resetPsd();
                    } else {
                        if(checkbox.isChecked())
                        {
                            setPsd();
                        }else{
                            ToastUtil.showToast("请仔细阅读协议");
                        }
                    }
                }
                break;
            case R.id.tv_read_and_agree2:
            case R.id.tv_read_and_agree:

                Intent intent=new Intent();
                intent.setClass(mContext,TextActivity.class);
                mContext.startActivity(intent);
                break;
        }
    }
}
